<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="userId" value="${param.staffId}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/MarketRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/UserRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ShopRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</head>
<body>
<form id="staffForm">
    <center>
        <input type="hidden" id="userId" name="userId" value="${userId}">
        <input type="hidden" id="userState" name="userState" value="1">
        <input type="hidden" id="userType" name="userType" value="41">
        <input type="hidden" id="userRemark" name="userRemark" value="车商员工">
        <input type="hidden" id="accountId" name="accountId">
        <table width="100%" cellpadding="5" cellspacing="5">
            <tr>
                <td>
                    所属市场:
                </td>
                <td>
                    <select id="marketId" name="marketId" onchange="getShopList()">
                    </select>
                </td>
                <td>
                    所属店铺:
                </td>
                <td>
                    <select id="shopId" name="shopId">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    用户电话:
                </td>
                <td>
                    <input type="tel" placeholder="用户电话(登录名,必填)" id="userMobile" name="userMobile" onchange="checkMobile()"  maxlength="11" required/>
                </td>
                <td>
                    用户姓名:
                </td>
                <td>
                    <input type="text" placeholder="用户姓名(必填)" id="userName" name="userName" maxlength="10" required/>
                </td>
                <td>
                    用户身份证:
                </td>
                <td>
                    <input type="text" placeholder="用户身份证" id="userNo" name="userNo" maxlength="18" />
                </td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #e1ecf5" colspan="6">
                </td>
            </tr>
            <tr>
                <td colspan="6" align="center">
                    <button type="button" onclick="insertOrUpdateStaff()">保存</button>
                    <button type="button" onclick="callbackStaff()">返回</button>
                </td>
            </tr>
        </table>
    </center>
</form>
</body>
</html>

<script>
    //    显示市场列表
    var market = new Market();
    market.marketId = "${marketId}";
    MarketRpcService.selectMarketList(market, callbackSelectMarketList);

    function callbackSelectMarketList(list) {
        dwr.util.addOptions("marketId", list, "marketId", "marketName");
        dwr.util.setValue("marketId", "${marketId}");
        if ("${userId}".length > 0) {
            selectUserList();
        } else {
            getShopList();
        }

    }

    function selectUserList() {
        var staff = new User();
        staff.userId = "${userId}";
        UserRpcService.selectUserList(staff, callbackSelectStaffList);
    }

    function getShopList(){
        var shop = new Shop();
        shop.marketId = dwr.util.getValue("marketId");
        ShopRpcService.selectShopList(shop, callbackSelectShopList);
    }

    function  callbackSelectShopList(list){
        dwr.util.removeAllOptions("shopId");
        dwr.util.addOptions("shopId", {"" : "请选择"});
        dwr.util.addOptions("shopId", list, "shopId", "shopName");
        dwr.util.setValue("shopId", "${shopId}");
        if ($("shopId").selectedIndex > 0) {
            $("shopId").disabled = true;
        }
        if(shopId) {
            dwr.util.setValue("shopId", shopId);
        }
    }

    var shopId

    function callbackSelectStaffList(list) {
        var staff = list[0]
        dwr.util.setValues(staff);
        getShopList();
        shopId = staff.shopId;
    }

    function checkMobile(){
        var user = new User();
        user.userState = 1;
        user.userMobile = dwr.util.getValue("userMobile");
        UserRpcService.selectUserList(user,callbackSelectMobileList);
        function callbackSelectMobileList(list){
            if (list.length > 0 ){
                dwr.util.setValue("userMobile",'');
                alert("手机号重复请重填");
            }
        }
    }

    function insertOrUpdateStaff() {
        if (!validate($("staffForm"))) {
            return false;
        }
        var staff = dwr.util.getValues("staffForm");
        if ("${userId}".length > 0) {
            UserRpcService.updateUser(staff, callbackStaff);
        } else {
            UserRpcService.insertUser(staff, callbackStaff);
        }
    }

    function callbackStaff() {
        location.href = "${ctx}/user/staff_list.jsp";
    }

</script>
